﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class ToolsDateTimeWinForm : Form
    {
        public ToolsDateTimeWinForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtBoxDate.Text)) {
                MessageBox.Show("请输入需要推算的天数！");
                return;
            }
            if (string.IsNullOrWhiteSpace(selectDate.Text))
            {
                MessageBox.Show("请选择往前还是往后推算日期！");
                return;
            }
            DateTime dt = dateTimeStatrt.Value;
            string selectVal = "-";
            if (selectDate.Text=="往前推") {
                selectVal = "-";
            }
            else {
                selectVal = "";
            }
            DateTime dateTimeDt = dt.AddDays(double.Parse(selectVal + txtBoxDate.Text));
            int year = dateTimeDt.Year;
            int month = dateTimeDt.Month;
            int day = dateTimeDt.Day;
            labDateCalendar.Text= year+"年"+ month + "月" + day + "日" + "  " + CaculateWeekDay(year,month,day);
            labCalendar.Text = SolarToChineseLunisolarDate(dateTimeDt);
        }
        /// <summary>
        /// 公历转为农历的函数
        /// </summary>
        /// <param name="solarDateTime">公历日期</param>
        /// <returns>农历的日期</returns>
        public string SolarToChineseLunisolarDate(DateTime solarDateTime)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

            int year = cal.GetYear(solarDateTime);
            int month = cal.GetMonth(solarDateTime);
            int day = cal.GetDayOfMonth(solarDateTime);
            int leapMonth = cal.GetLeapMonth(year);
            return string.Format("农历{0}{1}（{2}）年{3}{4}月{5}{6}"
                                , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
                                , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
                                , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
                                , month == leapMonth ? "闰" : ""
                                , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
                                , "初十廿三"[day / 10]
                                , "日一二三四五六七八九"[day % 10]
                                );
        }
        /// <summary>
        /// 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns></returns>
        public string CaculateWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            string weekstr = "";
            switch (week)
            {
                case 1: weekstr = "星期一"; break;
                case 2: weekstr = "星期二"; break;
                case 3: weekstr = "星期三"; break;
                case 4: weekstr = "星期四"; break;
                case 5: weekstr = "星期五"; break;
                case 6: weekstr = "星期六"; break;
                case 7: weekstr = "星期日"; break;
            }

            return weekstr;
        }
        /// <summary>
        /// 输入天数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBoxDate_KeyPress(object sender, KeyPressEventArgs e)
        {
            //开头不允许出现0
            if (txtBoxDate.SelectionStart == 0)
            {
                if (e.KeyChar == '0')
                {
                    e.Handled = true;
                    return;
                }
            }


            //只能
            if (e.KeyChar == '0'
                || e.KeyChar == '1'
                || e.KeyChar == '2'
                || e.KeyChar == '3'
                || e.KeyChar == '4'
                || e.KeyChar == '5'
                || e.KeyChar == '6'
                || e.KeyChar == '7'
                || e.KeyChar == '8'
                || e.KeyChar == '9'
                || e.KeyChar == '\b')
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 间隔日期确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDays_Click(object sender, EventArgs e)
        {
            DateTime dtStart = dateStart.Value;
            DateTime dtEnd = dateEnd.Value;
            labDays.Text = DateDiff(dtStart, dtEnd).ToString()+"天";
        }
        /// <summary>
        /// 计算时间差
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        private int DateDiff(DateTime dateStart, DateTime dateEnd)

        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());

            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

            TimeSpan sp = end.Subtract(start);

            return sp.Days;

        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolsDateTimeWinForm_Load(object sender, EventArgs e)
        {
            selectDate.DropDownStyle = ComboBoxStyle.DropDownList; //设置只读
            var list = new string[] { "往前推", "往后推" };
            foreach (var item in list)
            {
                selectDate.Items.Add(item);
            }
            selectDate.SelectedIndex =1;
        }
    }
}
